#include "worker.h"
#include "MySQL.h"
#include "taskQueue.h"
#include "user.h"
#include <mysql/mysql.h>
void *threadFunc(void *argv){                                                     
    threadPool_t *pThreadPool=(threadPool_t *)argv;                               
    while(1){                                                                     
        pthread_mutex_lock(&pThreadPool->mutex);    
        MYSQL *mysql=NULL;
        MySQLCon(&mysql,&pThreadPool->configUsrInfo);
        pthread_mutex_unlock(&pThreadPool->mutex);    
        pthread_mutex_lock(&pThreadPool->mutex);    
        while(!pThreadPool->exitFlag&&pThreadPool->taskQueue.queueSize<=0){       
            pthread_cond_wait(&pThreadPool->cond,&pThreadPool->mutex);            
        }                                                                         
        if(pThreadPool->exitFlag){                                                
            pthread_mutex_unlock(&pThreadPool->mutex);                            
            pthread_exit(NULL);                                                   
        }                                             
        usrInfo_t usrInfo=pThreadPool->taskQueue.pFront->usrInfo;                 
        deQueue(&pThreadPool->taskQueue);                                         
        pthread_mutex_unlock(&pThreadPool->mutex);    
            char buf[1024]={0};
            train_t train;
            /* int ret=recv(usrInfo.netFd,&train,sizeof(int),MSG_WAITALL); */
            /* ERROR_CHECK(ret,-1,"recv"); */
            printf("执行传输前\n");
            printf("工人拿到的netFd=%d\n",usrInfo.netFd);
            commandExcute(&usrInfo,&train,pThreadPool,mysql);
            while(1){
                sleep(1);
            }
    }
}

int makeWorker(threadPool_t *pThreadPool){
    for(int i=0;i<pThreadPool->tidArr.workerNum;++i){
        pthread_create(&pThreadPool->tidArr.pWorker[i],NULL,threadFunc,pThreadPool);
    }
    return 0;
}
